package com.mes.controller;

import com.mes.entity.User;
import com.mes.service.QualityDataService;
import com.yy.base.page.Message;
import com.yy.base.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("qualityData")
public class QualityDataController {
	@Autowired
	private QualityDataService qualityDataService;
	
	//加载工序信息
	@ResponseBody
	@RequestMapping(value="initProcessInfo",method=RequestMethod.POST)
	public Message initProcessInfo(HttpServletRequest request){
		Message msg = new Message();
		User user = UserUtil.getLoginUser(request);
		msg.setData("rows", qualityDataService.getProcessList(user));
		return msg;
	}
	//保存生产数据采集信息
	@ResponseBody
	@RequestMapping(value="saveQualityData",method=RequestMethod.POST)
	public Message saveQualityData(HttpServletRequest request){
		String code = request.getParameter("code");
		String manuOrderCode = request.getParameter("manuOrderCode");
		String manuBatchId = request.getParameter("manuBatchId");
		String processId = request.getParameter("processId");
		String description = request.getParameter("description");
		String results = request.getParameter("results");
		String codeType = request.getParameter("codeType");
		
		String qualityResultId = request.getParameter("qualityResultId");
		String codes = request.getParameter("codes");
		
		Message msg = new Message();
		User user = (User)request.getSession().getAttribute(UserUtil.USERSESSION);
		String result = qualityDataService.saveQualityData(code, manuOrderCode, manuBatchId, processId, description, 
				codeType, results, user, qualityResultId, (codes == null || "".equals(codes) ? "Y" : "N"));
		if("YES".equals(result)) {
			msg.setOkMessage("保存成功!");
		} else if("NO".equals(result)) {
			msg.setErrorMessage("保存失败!");
		} else {
			msg.setErrorMessage(result);
		}
		return msg;
	}

	//加载采集结果
	@ResponseBody
	@RequestMapping(value="getQualityResult", method=RequestMethod.POST)
	public Message getQualityResult(HttpServletRequest request, String manuBatchId){
		Message msg = new Message();
		Map<String, Object> map = qualityDataService.getQualityResult(manuBatchId);
		if (map == null) {
			msg.setErrorMessage("未进行检验数据录入");
		} else {
			msg.setData(map);
		}
		return msg;
	}
	//加载采集结果
	@ResponseBody
	@RequestMapping(value="submitQualityResult", method=RequestMethod.POST)
	public Message submitQualityResult(HttpServletRequest request, String codes){
		Message msg = new Message();
		String error = qualityDataService.submitQualityResult(codes);
		if (error != null) {
			msg.setErrorMessage(error);
		} else {
			msg.setOkMessage("提交成功！");
		}
		return msg;
	}

	//加载采集项信息
	@ResponseBody
	@RequestMapping(value="getProcessDCItems", method=RequestMethod.POST)
	public Message getProcessDCItems(HttpServletRequest request, String processId){
		Message msg = new Message();
		msg.setData("list", qualityDataService.getDcItems(processId));
		return msg;
	}

	//加载工序信息
	@ResponseBody
	@RequestMapping(value="getProcessList", method=RequestMethod.POST)
	public Message getProcessList(HttpServletRequest request){
		Message msg = new Message();
		String orgId = (String)request.getSession().getAttribute("orgId");
		List<Map<String, Object>> processList = qualityDataService.getProcessList(orgId);
		
		String fromMenu = request.getParameter("fromMenu");	//从哪打开页面
		if(processList != null && !"qualityData".equals(fromMenu)) {	//不是质量数量采集 菜单
			for (int i = processList.size(); i > 0; i--) {
				Map<String, Object> map = processList.get(i - 1);
				String pname = map.get("NAME") + "";
				if(!"EL检测".equals(pname) && !"测试分选".equals(pname) && !"外观全检".equals(pname)) {
					processList.remove(i - 1);
				}
			}
		}
		msg.setData("list", processList);
		String codes = request.getParameter("codes");
		if (codes != null && !"".equals(codes)) {
			msg.setData("codeStatus", qualityDataService.getCodeStatus(codes));
		}
		return msg;
	}
}
